<?php


namespace App\Http\Controllers\Admin;


use App\Http\Common\BaseController;
use App\Models\NavigationRoute;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class NavigationController extends BaseController
{
    /**
     * 创建导航栏
     * 未完成
     * @param Request $request
     * @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\Routing\ResponseFactory|\Illuminate\Http\Response
     */
    public function  creatNav(Request  $request){
        $name   = $request->input('name');
        $route   = $request->input('route');
        $component   = $request->input('component');
        //创建启动事务
        DB::beginTransaction();
        try {
            NavigationRoute::insert([
                'name' => $name,
                'route' => $route,
                'component' => $component,
                'create_time' => time()
            ]);

            $admin_id = $request->userId;
            $content = $this->mkNoticeContent($admin_id,'',$name . '导航栏',2);
            $this->creatNotice(6,$admin_id,$content);
            // 提交事务
            Db::commit();
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            return $this->returnJson(self::DATA_NOTFOUND,'该数据可能已被删除' );
        }
        return $this->returnJson(self::SUCCESS,'成功');
    }

    /**
     * 修改导航栏
     * 未完成
     * @param Request $request
     * @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\Routing\ResponseFactory|\Illuminate\Http\Response
     */
    public function  updateNav(Request  $request){
        $name   = $request->input('name');
        $id   = $request->input('id');
        $component   = $request->input('component');
        $route   = $request->input('route');

        $updateList = array();
        if (empty($id)) return $this->returnJson(self::DATA_NOTFOUND,'请传id' );
        if (!empty($name)) $updateList['name'] = $name;
        if (!empty($component)) $updateList['component'] = $component;
        if (!empty($route)) $updateList['route'] = $route;

        //创建启动事务
        DB::beginTransaction();
        try {
            NavigationRoute::where('id','=',$id)->update($updateList);

            $admin_id = $request->userId;
            $content = $this->mkNoticeContent($admin_id,'',$name . '导航栏',4);
            $this->creatNotice(6,$admin_id,$content);
            // 提交事务
            Db::commit();
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            return $this->returnJson(self::DATA_NOTFOUND,'错误',$e );
        }
        return $this->returnJson(self::SUCCESS,'成功');
    }

    /**
     * 获取所有导航栏
     * 未完成
     * @param Request $request
     * @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\Routing\ResponseFactory|\Illuminate\Http\Response
     */
    public function getAllNav(Request $request){
        $navList = NavigationRoute::get();
        foreach ($navList as $nav){
            $nav['create_time'] = $this->switchTime($nav['create_time'] );
        }
        return $this->returnJson(self::SUCCESS,'成功',$navList);
    }

    /**
     * 删除导航栏
     * @param Request $request
     * @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\Routing\ResponseFactory|\Illuminate\Http\Response
     */
    public function  delNav(Request $request){
        $admin_id = $request->userId;
        $id = $request->input('id');
        //创建启动事务
        DB::beginTransaction();
        try {
            $content = $this->mkNoticeContent($admin_id,'',NavigationRoute::where('id','=',$id)->pluck('name')[0] . ' 导航栏',3);

            NavigationRoute::where('id','=',$id)->delete();

            $this->creatNotice(6,$admin_id,$content);
            // 提交事务
            Db::commit();
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            return $this->returnJson(self::DATA_NOTFOUND,'该数据可能已被删除' );
        }
        return $this->returnJson(self::SUCCESS,'成功');
    }
    /**
     * 批量删除导航栏
     * @param Request $request
     * @return \Illuminate\Contracts\Foundation\Application|\Illuminate\Contracts\Routing\ResponseFactory|\Illuminate\Http\Response
     */
    public function  delNavMany(Request $request){
        $admin_id = $request->userId;
        $id_list = $request->input('id');
        if (!is_array($id_list)) $id_list = explode(',',$id_list);
        //创建启动事务
        DB::beginTransaction();
        try {
            foreach ($id_list as $id){
                $content = $this->mkNoticeContent($admin_id,'',NavigationRoute::where('id','=',$id)->pluck('name')[0] . ' 导航栏',3);

                NavigationRoute::where('id','=',$id)->delete();

                $this->creatNotice(6,$admin_id,$content);
            }
//             提交事务

            Db::commit();
        } catch (\Exception $e) {
            // 回滚事务
            Db::rollback();
            return $this->returnJson(self::DATA_NOTFOUND,'该数据可能已被删除' );
        }
        return $this->returnJson(self::SUCCESS,'成功');
    }
}
